package org.springcloud.wisdom.spring_01_canteen.dishes.fordishes;

import org.springcloud.wisdom.spring_01_canteen.common.bean.Page;
import org.springcloud.wisdom.spring_01_canteen.login.entity.Mall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishesService {
    @Autowired
    DishesDao dishesDao;
    //菜品展示
    public Page<Dishes> getOnrePage(int pageNum, int pageSize, String dname,Integer mallid){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Dishes> list=dishesDao.candish(start,pageSize,dname,mallid);
        //获取总条目数
        int total=dishesDao.countdish(dname,mallid);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Dishes> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
    //菜品展示
    public Page<Dishes> getOnrePage1(int pageNum, int pageSize, String dname){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Dishes> list=dishesDao.candish1(start,pageSize,dname);
        //获取总条目数
        int total=dishesDao.countdish1(dname);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Dishes> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    public List<Dishes> dish(String dname){
        return dishesDao.dishes(dname);
    }

    public List<Dishes> getDishesByMallId(Integer mallid) {
        return dishesDao.dishesByMallId(mallid);
    }

    //菜品增加
    public boolean adddish(Dishes dishes){
        if (dishes == null){
            return false;
        }
        return dishesDao.adddish(dishes)>0;
    }

    //菜品删除（根据did）
    public boolean deldish(Integer did){
        if (did==null){
            return false;
        }
        return dishesDao.deldish(did)>0;
    }

    //菜品修改
    public boolean updatedish(Dishes dishes){
        if (dishes == null){
            return false;
        }
        if (dishes.getDprice()==null || dishes.getDprice().trim().length()==0){
            return false;
        }
        return dishesDao.updatedish(dishes)>0;
    }

    //菜品修改
    public boolean update(Dishes dishes){
        if (dishes == null){
            return false;
        }

        return dishesDao.update(dishes)>0;
    }

    //修改菜品上下架状态
    public boolean approve(Integer did) {
        // 更新状态为“1---未锁定”
        return dishesDao.updateStatus(did, 1) > 0;
    }

    public boolean reject(Integer did) {
        // 更新状态为“0---已锁定”
        return dishesDao.updateStatus(did, 0) > 0;
    }

    //修改菜品审核状态
    //同意
    public boolean updateIsdel(Dishes dishes){
        dishes.setIsdel("已同意");
        System.out.println(dishes);
        return dishesDao.update(dishes)>0;
    }
    //拒绝
    public boolean updateIsdel1(Dishes dishes){
        dishes.setIsdel("已拒绝");
        System.out.println(dishes);
        return dishesDao.update(dishes)>0;
    }
    //查看商品数量
    public int countDishesByMallId(Integer mallid) {
        return dishesDao.countDishesByMallId(mallid);
    }
}
